package org.zgflex.desktop.selection
{
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	
	import org.zgflex.desktop.ArrayIterator;
	import org.zgflex.desktop.IIconSelector;

	/**
	 * A base class for a selection renderer.<br/>
	 * Selection renderer draws a rectangle behind each selected icon.
	 * @author dankok
	 * 
	 */	
	public class SelectionRendererBase implements ISelectionRenderer
	{
		/**
		 * Constructor 
		 * @param target
		 * 
		 */		
		public function SelectionRendererBase(target:IIconSelector = null)
		{
			super();
			
			if (target)
				this.target = target;
		}
		
		/**
		 * Selection background color. 
		 */		
		public var backgroundColor:uint = 0xff66ff;
		
		/**
		 * Selection background alpha. 
		 */		
		public var backgroundAlpha:Number = 0.8;
		
		/**
		 * Selection border color. 
		 */		
		public var borderColor:uint = 0xffff00;
		
		/**
		 * Selection border alpha. 
		 */		
		public var borderAlpha:Number = 0.8;
		
		/**
		 * Selection border width. 
		 */		
		public var borderWidth:uint = 4;
		
		/**
		 * Selection horizontal inflation. 
		 */		
		public var inflationX:Number = 6;
		
		/**
		 * Selection vertical inflation. 
		 */	
		public var inflationY:Number = 6;
		
		/**
		 * @private 
		 */
		protected var _previouslySelectedIcons:ArrayCollection = new ArrayCollection();
		
		/**
		 * @private 
		 */
		protected var _moved:Boolean = false;
		
		/**
		 * @private 
		 */		
		protected var _target:IIconSelector;
		/**
		 * A reference to desktop (required for computing desktop size, for capturing events etc.) 
		 * @inheritDoc
		 * 
		 */
		public function set target(value:IIconSelector):void {
			_target = value;
		}
		public function get target():IIconSelector {
			if (_target == null)
				throw new Error("target not defined");
			return _target;
		}
		
		/**
		 * @private 
		 */
		protected var _surface:Canvas;
		/**
		 * @inheritDoc
		 * 
		 */
		public function set surface(value:Canvas):void {
			_surface = value;
		}
		public function get surface():Canvas {
			if (_surface == null)
				throw new Error("surface not defined");
			return _surface;
		}
		
		/**
		 * @private 
		 */
		protected var _origin:Point;
		
		/**
		 * @inheritDoc
		 * 
		 */		
		public function update():void
		{
			//trace("selection renderer update");
			surface.graphics.clear(); // needed to be always executed because of the deletion of current selections 
			var it:ArrayIterator = target.selectedIconsIterator;
			if (it.count > 0){
				var icon:DisplayObject;
				while (it.hasNext()){ // draws a rectangle behind each selected icon
					icon = DisplayObject(it.next());
					var rect:Rectangle = icon.getBounds(surface);
					rect.inflate(inflationX, inflationY)
					draw(rect);
				}
			}			
		}
		
		/**
		 * Draws the selection rectangle 
		 * @param rect
		 * 
		 */		
		protected function draw(rect:Rectangle):void {			
			// abstract
		}
	}
}